import 'package:getx_mvvm_lite/core/network/api_client.dart';
import 'package:getx_mvvm_lite/core/state/result.dart';
import 'package:getx_mvvm_lite/features/todo/todo_model.dart';

abstract class TodoRepository {
  Future<Result<List<Todo>>> fetchTodos({required int page, required int pageSize});
}

class ApiTodoRepository implements TodoRepository {
  final ApiClient client;
  ApiTodoRepository(this.client);

  @override
  Future<Result<List<Todo>>> fetchTodos({required int page, required int pageSize}) async {
    try {
      final res = await client.get<List<dynamic>>('/todos', queryParameters: {
        '_page': page,
        '_limit': pageSize,
      });
      final list = (res.data ?? [])
          .map((json) => Todo.fromJson(json as Map<String, dynamic>))
          .toList();
      return Result.ok(list);
    } catch (e) {
      return Result.fail(e.toString());
    }
  }
}